﻿Imports System
Imports System.Runtime.InteropServices

Public Class clsIme

    Private Declare Function ImmGetContext Lib "imm32.dll" (ByVal hwnd As System.IntPtr) As System.IntPtr
    Private Declare Function ImmGetOpenStatus Lib "imm32.dll" (ByVal hwnd As System.IntPtr) As Boolean
    Private Declare Function ImmGetConversionStatus Lib "imm32.dll" (ByVal himc As System.IntPtr, ByRef lpdw As Integer, ByRef lpdw2 As Integer) As Boolean
    Private Declare Function ImmSimulateHotKey Lib "imm32.dll" (ByVal hwnd As System.IntPtr, ByVal dw As Integer) As Integer

    Public Sub AddHandler_ImeMode(ByRef pans() As Control)
        For Each ctrl As Control In pans
            For Each p As Control In ctrl.Controls
                AddHandler p.Enter, AddressOf ControlIME
            Next
        Next

    End Sub

    Public Sub ControlIME(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim HIme As System.IntPtr = ImmGetContext(sender.Handle)
        If ImmGetOpenStatus(HIme) Then
            Dim iMode As Integer = 0
            Dim iSentence As Integer = 0
            Dim bSuccess As Boolean = ImmGetConversionStatus(HIme, iMode, iSentence)
            If bSuccess Then
                If (iMode And &H8) > 0 Then
                    ImmSimulateHotKey(sender.Handle, &H11)
                End If
            End If
        End If

    End Sub

End Class
